﻿using System;
using System.Linq;
using NullableFields.Model;
using NUnit.Framework;
using Xtensive.Orm;
using Xtensive.Orm.Configuration;

namespace NullableFields
{
    public class Program
    {
        [Test]
        public static void Main()
        {
            // Loading domain configuration section with name "Default".
            var domainConfiguration = DomainConfiguration.Load("Default");
            var domain = Domain.Build(domainConfiguration);

            using (var session = domain.OpenSession()) {
                using (var t = session.OpenTransaction()) {
                    new Book(session) {
                        Title = "DataObjects.Net unleashed",
                        PublicationYear = 2013
                    };

                    new Book(session) {
                        Title = "Bible",
                        PublicationYear = null // Unknown year for ancient books
                    };

                    t.Complete();
                }

                using (var tx = session.OpenTransaction()) {
                    Console.WriteLine("Ancient books:");

                    var ancientBooks = session.Query.All<Book>()
                        .Where(b => b.PublicationYear==null)
                        .ToList();

                    foreach (var book in ancientBooks) {
                        Console.WriteLine(book.Title);
                    }

                    Console.WriteLine("Modern books:");

                    var modernBooks = session.Query.All<Book>()
                        .Where(b => b.PublicationYear > 2010)
                        .ToList();

                    foreach (var book in modernBooks) {
                        Console.WriteLine(book.Title);
                    }

                    tx.Complete();
                }
            }
        }
    }
}
